package com.egoo.ticket.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egoo.eeip.eeif.framework.base.util.date.DateTime;
import com.egoo.ticket.server.dao.EwsUserDao;
import com.egoo.ticket.server.dao.EwsUserRoleDao;
import com.egoo.ticket.server.dao.MsgTemplateMapper;
import com.egoo.ticket.server.pojo.dto.MsgTemplateDto;
import com.egoo.ticket.server.pojo.entity.EwsUser;
import com.egoo.ticket.server.pojo.entity.MsgTemplate;
import com.egoo.ticket.server.pojo.vo.MsgTemplateVo;
import com.egoo.ticket.server.service.IMsgTemplateService;
import com.egoo.ticket.server.service.UserService;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author donny
 * @since 2020-10-27
 */
@Service
@Log4j2
public class MsgTemplateServiceImpl extends ServiceImpl<MsgTemplateMapper, MsgTemplate> implements IMsgTemplateService {
	@Autowired
	MsgTemplateMapper msgTemplateMapper;
	@Autowired
	EwsUserDao ewsUserDao;
	@Autowired
	UserService userService;

	/**
	 * @Author: Donny_dong
	 * @Description: 新增消息模板
	 * @Param:
	 * @return:
	 * @Date: 2020/10/27
	 */
	@Override
	public DbResponse insertTemplate(MsgTemplateDto msgTemplateDto) {
		try {
			MsgTemplate msgTemplate = new MsgTemplate();
			String name = userService.selectNameById(msgTemplateDto.getUserId());
			BeanUtils.copyProperties(msgTemplateDto,msgTemplate);
			Date date = new Date();
			msgTemplate.setCreateTime(date)
						.setStatus(1)
						.setCreator(msgTemplateDto.getUserId())
						.setUpdaterName(msgTemplateDto.getUserId())
						.setUpdateTime(date);
			msgTemplateMapper.insert(msgTemplate);
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.ADD_SUCCESS_MSG);
		} catch (BeansException e) {
			log.info("添加遗产"+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.ADD_FAIL_MSG);
		}

	}

	/**
	 * @Author: Donny_dong
	 * @Description: 获取用户名
	 * @Param:
	 * @return:
	 * @Date: 2020/10/27
	 */
	private String getUserName(String userId) {
		EwsUser ewsUser = ewsUserDao.selectById(userId);
		String name=null;
		if (!ObjectUtils.isEmpty(ewsUser)){
			name = ewsUser.getName();
		}
		return name;
	}
	/**
	 * @Author: Donny_dong
	 * @Description: 编辑或启用
	 * @Param:
	 * @return:
	 * @Date: 2020/10/27
	 */
	@Override
	public DbResponse updateTemplate(MsgTemplateDto msgTemplateDto) {
		try {
			String name = userService.selectNameById(msgTemplateDto.getUserId());
			MsgTemplate msgTemplate = msgTemplateMapper.selectById(msgTemplateDto.getId());

				msgTemplate.setName(msgTemplateDto.getName())
							.setUpdateTime(new Date())
							.setUpdaterName(msgTemplateDto.getUserId())
							.setDiscription(msgTemplateDto.getDiscription())
							.setContent(msgTemplateDto.getContent())
							.setTemplateType(msgTemplateDto.getTemplateType())
							.setTitle(msgTemplateDto.getTitle())
							.setAttachmentAddress(msgTemplateDto.getAttachmentAddress())
							.setLinkToWorkOrder(msgTemplateDto.getLinkToWorkOrder());

			QueryWrapper<MsgTemplate> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(MsgTemplate::getId,msgTemplateDto.getId());
			int update = msgTemplateMapper.update(msgTemplate, queryWrapper);
			if (update<1){
				return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.UPDATE_FAIL_MSG);
			}
		} catch (Exception e) {
			log.info("更新模板异常"+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"更新异常");
		}
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.UPDATE_SUCCESS_MSG);
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 列表查询
	 * @Param:
	 * @return:
	 * @Date: 2020/10/27
	 */
	@Override
	public List<MsgTemplateVo> getList(MsgTemplateDto msgTemplateDto) {

		Integer pageNumber = msgTemplateDto.getPageNumber();
		Integer pageSize = msgTemplateDto.getPageSize();
//		QueryWrapper<MsgTemplate> queryWrapper = new QueryWrapper<>();
//		if (StringUtils.isEmpty(msgTemplateDto.getSearchContent())) {
//
//			queryWrapper.lambda().eq(MsgTemplate::getTenantId, msgTemplateDto.getTenantId());
//		}else {
//
//			queryWrapper.lambda().eq(MsgTemplate::getTenantId, msgTemplateDto.getTenantId()).like(MsgTemplate::getName,msgTemplateDto.getSearchContent());
//		}

//		Page<MsgTemplate> page = new Page<>(msgTemplateDto.getPageNumber(),msgTemplateDto.getPageSize());
//		IPage<MsgTemplate> msgTemplateIPage = msgTemplateMapper.selectPage(page, queryWrapper);
		PageHelper.startPage(pageNumber,pageSize);
		List<MsgTemplateVo> list = msgTemplateMapper.getList(msgTemplateDto);

		return list;



	}
}
